package com.maoye.ossp.sys.base.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.base.entity.SysBaseBussinessArea;
import com.maoye.ossp.sys.base.entity.SysBaseFormat;
import com.maoye.ossp.sys.base.entity.SysBaseShop;
import com.maoye.ossp.sys.base.entity.SysBaseStorageLocation;
import com.maoye.ossp.sys.base.service.SysBaseFormatService;
import com.maoye.ossp.sys.base.service.SysBaseShopService;
import com.maoye.ossp.sys.base.service.SysBaseStorageLocationService;
/**
 * 库存位置和业态controller类
 * @author liangtao
 * @date   2014-5-5f
 */
@Controller
@RequestMapping(value = "sys/base/storageFormat/")
public class SysBaseStorageLocationFormatController extends BaseController {
	@Resource
	private SysBaseStorageLocationService sysBaseStorageLocationService; 
	
	@Resource
	private SysBaseShopService sysBaseShopService;
	
	@Resource
	private SysBaseFormatService sysBaseFormatService; 
	
	/**
	 * 获得库存地点列表
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value = "getStorageLocationList")
	public String getStorageLocationList(SysBaseStorageLocation sysBaseStorageLocation,
			HttpServletRequest request, Model model) {
		QueryFilter queryFilter = getQuerFilter(request,sysBaseStorageLocation);
		Page<SysBaseStorageLocation> page = (Page<SysBaseStorageLocation>) sysBaseStorageLocationService
				.find(queryFilter);
		model.addAttribute("page", page);
		model.addAttribute("sysBaseStorageLocation",sysBaseStorageLocation);
		return resultPage("/sys/base/storageLocation/getStorageLocationList", model, request);
	}
	
	@RequestMapping(value = "addStorageLocation")
	public String addStorageLocation(HttpServletRequest request, Model model)
			throws IOException {
		Map map = new HashMap();
		SysBaseStorageLocation sysBaseStorageLocation = new SysBaseStorageLocation();
		model.addAttribute("sysBaseStorageLocation",sysBaseStorageLocation);
		return "/sys/base/storageLocation/add";
	}
	/**
	 * 验证库存地点编码
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value ="checkStorageLocationCode")
	public void checkStorageLocationCode(HttpServletRequest request, HttpServletResponse response) throws IOException{
		Map map = new HashMap();
		String code = request.getParameter("code");	
		map.put("code",code);
		PrintWriter out = response.getWriter();
		JSONObject json = new JSONObject();
		if(sysBaseStorageLocationService.checkStorageLocationCode(map)){
			json.put("resCode",0); //商圈编号可用
		}else{
			json.put("resCode",1);//商圈编号不可以用
		}
		out.print(json.toString());
		out.flush();
		out.close();
	}
	
   
	/**
	 * 保存库存地点信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value = "saveStorageLocation")
	public String saveStorageLocation(SysBaseStorageLocation sysBaseStorageLocation,HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes) throws IOException{
		sysBaseStorageLocation.setId(idGenerator.getUId());
		sysBaseStorageLocation.setCode(sysBaseStorageLocation.getCode().replaceAll(",","").toString());
		//设置创建人
		sysBaseStorageLocation.setCreateBy("");
		//设置修改人
		sysBaseStorageLocation.setUpdateBy("");
		Map map=new HashMap();
		map.put("code",sysBaseStorageLocation.getShopCode());
		SysBaseShop sysBaseShop=null;
		sysBaseShop=sysBaseShopService.getShopById(map);
		if(sysBaseShop!=null){
			sysBaseStorageLocation.setRegionCode(sysBaseShop.getRegionCode());
		}
		sysBaseStorageLocationService.saveStorageLocation(sysBaseStorageLocation);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:getStorageLocationList";
	}
	
	/*
	*//**
	 * 查看库存地点信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value ="viewStorageLocation")
	public String viewStorageLocation(HttpServletRequest request, HttpServletResponse response,Model model){
		String code =request.getParameter("code");
		String shopName=request.getParameter("shopName");
		Map map = new HashMap();
		map.put("code",code);
		SysBaseStorageLocation storageLocation=null;
		storageLocation=sysBaseStorageLocationService.getStorageLocationByCode(map);
		storageLocation.setShopName(shopName);
		model.addAttribute("storageLocation",storageLocation);
		return "/sys/base/storageLocation/view";
	}
	
	@RequestMapping(value ="updateStorageLocation")
	public String updateStorageLocation(HttpServletRequest request, HttpServletResponse response,Model model){
		String code =request.getParameter("code");
		String shopName=request.getParameter("shopName");
		Map map = new HashMap();
		map.put("code",code);
		SysBaseStorageLocation storageLocation=null;
		storageLocation=sysBaseStorageLocationService.getStorageLocationByCode(map);
		model.addAttribute("sysBaseStorageLocation",storageLocation);
		model.addAttribute("shopName",shopName);
		return "/sys/base/storageLocation/update";
	}
	
	/*
	*//**
	 * 保存修改库存地点信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value ="saveUpdateStorageLocation")
	public String saveUpdateStorageLocation(HttpServletRequest request,HttpServletResponse response,SysBaseStorageLocation storageLocation,RedirectAttributes redirectAttributes){
		//设置创建人
		storageLocation.setCreateBy("");
		//设置修改
		storageLocation.setUpdateBy("");
		Map map=new HashMap();
		map.put("code",storageLocation.getShopCode());
		SysBaseShop sysBaseShop=null;
		sysBaseShop=sysBaseShopService.getShopById(map);
		if(sysBaseShop!=null){
			storageLocation.setRegionCode(sysBaseShop.getRegionCode());
		}
		sysBaseStorageLocationService.saveUpdateStorageLocation(storageLocation);
		addMessage(redirectAttributes, "修改成功");
		return "redirect:getStorageLocationList";
	}
	
	/*
	*//**
	 * 删除库存地点信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */ 
	@RequestMapping(value ="removeStorageLocation")
	public String removeStorageLocation(HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes){
		Map map = new HashMap();
		String code=request.getParameter("code");
		map.put("code",code);
		sysBaseStorageLocationService.removeStorageLocation(map);
		addMessage(redirectAttributes, "删除成功");
		return "redirect:getStorageLocationList";
	}
	
	
	/**
	 * 获得业态信息列表
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value = "getFormatList")
	public String getFormatList(SysBaseFormat sysBaseFormat,
			HttpServletRequest request, Model model) {
		QueryFilter queryFilter = getQuerFilter(request,sysBaseFormat);
		Page<SysBaseFormat> page = (Page<SysBaseFormat>) sysBaseFormatService
				.find(queryFilter);
		model.addAttribute("page", page);
		model.addAttribute("sysBaseFormat",sysBaseFormat);
		return resultPage("/sys/base/format/getFormatList", model, request);
	}
	
	@RequestMapping(value = "addFormat")
	public String addFormat(HttpServletRequest request, Model model)
			throws IOException {
		Map map = new HashMap();
		SysBaseFormat sysBaseFormat = new SysBaseFormat();
		model.addAttribute("sysBaseFormat",sysBaseFormat);
		return "/sys/base/format/add";
	}
	/**
	 * 验证业态编码
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value ="checkFormatCode")
	public void checkFormatCode(HttpServletRequest request, HttpServletResponse response) throws IOException{
		Map map = new HashMap();
		String code = request.getParameter("code");	
		map.put("code",code);
		PrintWriter out = response.getWriter();
		JSONObject json = new JSONObject();
		if(sysBaseFormatService.checkFormatCode(map)){
			json.put("resCode",0); //商圈编号可用
		}else{
			json.put("resCode",1);//商圈编号不可以用
		}
		out.print(json.toString());
		out.flush();
		out.close();
	}
	
   
	/**
	 * 保存业态信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value = "saveFormat")
	public String saveFormat(SysBaseFormat sysBaseFormat,HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes) throws IOException{
		sysBaseFormat.setId(idGenerator.getUId());
		//设置创建人
		sysBaseFormat.setCreateBy("");
		//设置修改人
		sysBaseFormat.setUpdateBy("");
		sysBaseFormatService.saveFormat(sysBaseFormat);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:getFormatList";
	}
	
	
	/*
	*//**
	 * 查看库存地点信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value ="viewFormat")
	public String viewFormat(HttpServletRequest request, HttpServletResponse response,Model model){
		String code =request.getParameter("code");
		Map map = new HashMap();
		map.put("code",code);
		SysBaseFormat format=null;
		format=sysBaseFormatService.getFormatByCode(map);
		model.addAttribute("format",format);
		return "/sys/base/format/view";
	}
	
	@RequestMapping(value ="updateFormat")
	public String updateFormat(HttpServletRequest request, HttpServletResponse response,Model model){
		String code =request.getParameter("code");
		Map map = new HashMap();
		map.put("code",code);
		SysBaseFormat format=null;
		format=sysBaseFormatService.getFormatByCode(map);
		model.addAttribute("sysBaseFormat",format);
		return "/sys/base/format/update";
	}
	
	/*
	*//**
	 * 保存修改业态信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value ="saveUpdateFormat")
	public String saveUpdateFormat(HttpServletRequest request,HttpServletResponse response,SysBaseFormat sysBaseFormat,RedirectAttributes redirectAttributes){
		//设置修改
		sysBaseFormat.setUpdateBy("");
		sysBaseFormatService.saveUpdateFormat(sysBaseFormat);
		addMessage(redirectAttributes, "修改成功");
		return "redirect:getFormatList";
	}
	
	/*
	*//**
	 * 删除库存地点信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */ 
	@RequestMapping(value ="removeFormat")
	public String removeFormat(HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes){
		Map map = new HashMap();
		String code=request.getParameter("code");
		map.put("code",code);
		sysBaseFormatService.removeFormat(map);
		addMessage(redirectAttributes, "删除成功");
		return "redirect:getFormatList";
	}
	
}
